package com.corecloud.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.corecloud.entity.SysPermissionEntity;
import com.corecloud.entity.SysPermissionRoleEntity;
import com.corecloud.entity.SysRoleEntity;
import com.corecloud.service.SysPermissionRoleService;
import com.corecloud.service.SysPermissionService;
import com.corecloud.service.SysRoleService;
import com.corecloud.tools.AjaxResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色与权限相关(授权等)
 */
@Controller
@RequestMapping("/authorization")
public class RolePermissionController {

	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysPermissionRoleService sysPermissionRoleService;
	@Autowired
	private SysPermissionService sysPermissionService;





	/**
	 * 授权
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView authorization(Principal principal, Integer rid){
		ModelAndView modelAndView = new ModelAndView("rolePermission/index");
		//查询信息,回填页面
		if(!StringUtils.isEmpty(rid)){
			modelAndView.addObject("roleInfo",sysRoleService.getByParas(new QueryWrapper<SysRoleEntity>().eq("id",rid)));
		}
		return modelAndView;
	}



	/**
	 * 获取权限结构树
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/tree")
	@ResponseBody
	public List<Map> tree(Principal principal, Integer rid){
		//获取所有菜单类型,已启用的权限
		QueryWrapper qw = new QueryWrapper<SysPermissionEntity>();
		qw.eq("is_valid",1);
//		qw.eq("type",0);
		List<Map<String,Object>> permissionList = sysPermissionService.getMapByQuery(qw);
		//获取所有角色与权限关联信息
		qw = new QueryWrapper<SysPermissionRoleEntity>();
		qw.eq("permission_state",1);
		qw.eq("rid",rid);
		List<SysPermissionRoleEntity> sprList = sysPermissionRoleService.getListByParas(qw);
		//比对拼装树形结构数据
		Map dataMap = new HashMap();
		List<Map> resultList = new ArrayList<>();
		for(int i = 0;i< permissionList.size();i++){
			Map<String,Object> permissionMap = permissionList.get(i);
			dataMap = new HashMap();
			dataMap.put("id",permissionMap.get("id"));
			dataMap.put("name",permissionMap.get("name"));
			dataMap.put("pid",permissionMap.get("parent_id") == null ? 0 : permissionMap.get("parent_id"));
			//设置默认选中参数
			for(int j = 0;j<sprList.size();j++){
				SysPermissionRoleEntity spr = sprList.get(j);
				if(spr.getPid() == Integer.parseInt(dataMap.get("id").toString())){
					dataMap.put("checked",true);
				}
			}
			resultList.add(dataMap);
		}
		return resultList;
	}



	/**
	 * 授权
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxResultData auzInfo(Principal principal, String pids,Integer rid){
		//角色ID必不可少
		if(StringUtils.isEmpty(rid)){
			return new AjaxResultData(500,"参数异常,请联系管理员");
		}
		try {
			if(StringUtils.isEmpty(pids)){
				//取消该角色所有权限(仅限菜单级权限)
				sysPermissionRoleService.deleteInfo(new QueryWrapper<SysPermissionRoleEntity>().eq("rid",rid).eq("permission_type",0));
			}else{
				String[] pidArr = pids.split(",");
				//删除原有所有权限,重新添加(仅限菜单级权限)
				sysPermissionRoleService.deleteInfo(new QueryWrapper<SysPermissionRoleEntity>().eq("rid",rid).eq("permission_type",0));
				SysRoleEntity sr = sysRoleService.getByParas(new QueryWrapper<SysRoleEntity>().eq("id",rid));
				List<SysPermissionEntity> spList = sysPermissionService.getListByParas(new QueryWrapper<SysPermissionEntity>().in("id", (Object[]) pidArr));
				SysPermissionRoleEntity spr ;
				for(int i = 0 ;i < spList.size();i++){
					SysPermissionEntity sp = spList.get(i);
					spr = new SysPermissionRoleEntity();
					spr.setPermissionName(sp.getName());
					spr.setPermissionState(sp.getIsValid());
					spr.setPermissionUrl(sp.getUrl());
					spr.setPermissionType(sp.getType());
					spr.setPid(sp.getId());
					spr.setRid(sr.getId());
					spr.setRoleCode(sr.getCode());
					spr.setRoleName(sr.getName());
					sysPermissionRoleService.saveInfo(spr);
				}
			}
			return new AjaxResultData("操作成功");
		}catch (Exception e){
			return new AjaxResultData(500,"操作失败,请联系管理员");
		}
	}
}
